package hust.rpc.serialize;

import hust.rpc.constants.Constants;
import hust.rpc.exception.MethodInvokeException;
import hust.rpc.serialize.json.JsonSerializer;
import hust.rpc.serialize.kryo.KryoSerializer;

import java.util.HashMap;
import java.util.Map;

public class SerializeUtils {

    private static final Map<Byte, Serializer> cache = new HashMap<>();

    static {
        cache.put(Constants.CODEC_JSON, new JsonSerializer());
        cache.put(Constants.CODEC_KRYO, new KryoSerializer());
    }

    public static Serializer getSerializer(byte codec) {
        Serializer serializer = cache.get(codec);
        if (serializer == null) throw new MethodInvokeException("找不到对应编号的序列化器: " + codec);
        return serializer;
    }
}
